<?php
/**
 * 匠言知识付费
 * Author: 山西匠言网络科技有限公司
 * This is not a free software, it under the license terms, you can visit https://www.zsffzxkc.cn/ get more details.
 */
namespace app\index\controller;
use think\Db;
use think\Session;
use think\Cache;
use aldy\SmsDemo;

/**
 * Class 代理商业务类
 * @package app\index\controller
 */
class Agent extends  Base
{
    public function yhmjh(){
        $post=input('post.');
        $map=array(
            'acid'=>1,
        );
        $yhminfo=db('yhm')->where($map)->where(['num'=>$post['yhm'],'is_jh'=>0])->find();
        if(!empty($yhminfo)){
            $pici=db('yhm_pici')->where($map)->where(['id'=>$yhminfo['pici_id'],'goodstype'=>$post['goodstype'],'goodsid'=>$post['goodsid']])->find();
            if(!empty($pici)){
                $this->result(0,'优惠码验证成功',$pici,0,'json');
            }else{
                $this->result(1,'非本商品优惠码,请重试','',0,'json');
            }
        }else{
            $this->result(1,'优惠码不正确,请重试','',0,'json');
        }
    }
    public function dls_info(){
        $post=input('post.');
        $map=array(
            'acid'=>1,
        );
        $dlsinfo=Db::name('user')
            ->where($map)
            ->where('id',$post['uid'])
            ->find();
        $this->result(0,'获取代理商信息成功',$dlsinfo,0,'json');
    }
    public function dls_jhm()
    {
        $post=input('post.');
        $map=array(
            'acid'=>1,
        );
        if($post['jhmtype']=='jhm'){
            $db=Db::name('jhm');
            $picidb=Db::name('jhm_pici');
        }else{
            $db=Db::name('yhm');
            $picidb=Db::name('yhm_pici');
        }
        $is_jh=$post['is_jh'];
        $jhms=$db->where($map)->where(['fxsuid'=>$post['uid'],'is_jh'=>$is_jh,'is_fp'=>1])->select();
        if(!empty($jhms)){
            foreach ($jhms as $k=>$v){
                $pici=$picidb->where($map)->where('id',$v['pici_id'])->find();
                if($post['jhmtype']=='jhm'){
                    $goodsinfo=Db::name('viptime')->where($map)->where('id',$pici['viptimeid'])->find();
                }elseif($post['jhmtype']=='yhm'){
                    if($pici['goodstype']=='course'){
                        $goodsinfo=Db::name('coursemenu')->where($map)->where('id',$pici['goodsid'])->find();
                    }else{
                        $goodsinfo=Db::name('goods')->where($map)->where('id',$pici['goodsid'])->find();
                    }
                }
                $jhms[$k]['goodsinfo']=$goodsinfo;
                if($is_jh=='1'){
                    $user=Db::name('user')->where($map)->where('id',$v['uid'])->find();
                    $jhms[$k]['user']=$user;
                    $jhms[$k]['jhtime']=date('Y-m-d H:i:s',$v['hexiaotime']);
                }
            }
            $this->result(0,'获取激活码信息成功',$jhms,0,'json');
        }else{
            $this->result(1,'暂无激活码','',0,'json');
        }
    }

    public function sendcode(){
        $Sms= new SmsDemo();
        $phoneNum=input('post.phone');
        $code = rand(100000,999999);
        $signName="知识付费在线课程";
        $TemplateCode=config('alidy.TemplateCode');
        $data = $Sms->sendSms($phoneNum,$signName,$TemplateCode,$code);
        if($data){
            $options = [
                // 缓存类型为File
                'type'   => 'File',
                // 缓存有效期为永久有效
                'expire' => 3600,
                // 指定缓存目录
                'path'   => APP_PATH . 'runtime/cache/',
            ];
            cache($options);
            cache('code', $code);
            cache('phone', $phoneNum);
            cache('codetime', time());
            $this->result(0,'发送成功',$data,0,'json');
        }
    }
    public function agent_reg(){
        $map=array(
            'acid'=>1
        );
        $post=input('post.');
        $phone=$post['phone'];
        if($phone!=''){
            if (time()-cache('codetime')>300) {
                cache('code', NULL);
                $this->result(3,'验证码超时失效！',array('codetime'=>cache('codetime')),0,'json');
            }else{
                if (cache('code')!=$post['code']) {
                    $this->result(1,'验证码不正确！','',0,'json');
                }elseif (cache('phone')!=$post['phone']){
                    $this->result(1,'提交手机号非验证手机号,请重试！','',0,'json');
                }else{
                    $phone_reg=Db::name('user')
                        ->where($map)
                        ->where('id',$post['uid'])
                        ->find();
                    $data=array(
                        'dls_name'=>$post['name'],
                        'dls_phone'=>$post['phone'],
                        'dls_sh'=>1,
                    );
                    if(!empty($phone_reg)){
                        $res=Db::name('user')->where($map)->where('id',$post['uid'])->update($data);
                        if($res){
                            $this->result(0,'请等待审核！','',0,'json');
                        }
                    }
                }
            }
        }
    }
}
